//
//  BackupsSwitchView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/27.
//

import SwiftUI

//温馨提示弹窗
struct CommonEditDialogView: View {
    let title :String
    let hint :String
    let content :String
    let uiImage:UIImage?
    let allowEmpty : Bool
    let dismiss: (String) -> Void
    let dismissCancel: () -> Void
    @State private var text = ""
    @FocusState private var isFocused: Bool // 控制键盘焦点

    var body: some View {
        PopupCenterView(title: title, dismiss: {
            dismissCancel()
        }) {
            ZStack {
                // 弹窗内容
                VStack(spacing: 20) {
                    if uiImage != nil {
                        Image(uiImage: uiImage!)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 150, height: 150)  // 固定尺寸
                    }
                    ZStack(alignment: .leading) {
                        if text.isEmpty {
                            Text(hint)
                                .foregroundColor(.gray)
                                .font(.system(size: 14))
                        }
                        
                        TextField("", text: $text)
                            .font(.system(size: 14))
                            .foregroundColor(.bgItemTitle)
                            .textFieldStyle(PlainTextFieldStyle())
                            .scrollContentBackground(.hidden)
                            .focused($isFocused) // 绑定焦点状态
                            .submitLabel(.done) // 显示「完成」按钮
                            .onSubmit {
                                isFocused = false
                            }.frame(height: 35)
                        
                    }.padding(.vertical, 5)
                        .padding(.horizontal,10)
                        .frame(maxWidth: .infinity)
                        .background(.bgEdit)
                        .cornerRadius(5)
                        .padding(.top,10)
                        .padding(.horizontal,10)
                    HStack(spacing: 20) {
                        Text("取消")
                            .font(.system(size: 14))
                            .frame(maxWidth: .infinity)
                            .frame(height: 46)
                            .background(themeManager.themeColor.opacity(0.2))
                            .foregroundColor(themeManager.themeColor)
                            .cornerRadius(10)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                dismissCancel()
                            }
                        
                        Text("确定")
                            .font(.system(size: 14))
                            .frame(maxWidth: .infinity)
                            .frame(height: 46)
                            .background(themeManager.themeColor)
                            .foregroundColor(.white)
                            .cornerRadius(10)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                if (text).isEmpty && allowEmpty == false {
                                    showToast("内容不能为空")
                                    return
                                }
                                dismiss(text)
                            }
                    }
                    .padding(.horizontal, 10)
                }
                .onAppear{
                    text = content
                }
            }
        }
    }
}

//#Preview {
//    CommonEditDialogView()
//}
//定义预览提供者
struct CommonEditDIalogView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: CommonEditDialogView(title: "测试", hint: "" ,content: "", uiImage: nil, allowEmpty: false){it in} dismissCancel: { })
    }
}
